import os, math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import precision_recall_curve, average_precision_score
import matplotlib.pyplot as plt
import seaborn as snszzzz
import helperFunctions
from collections import Counter
from sklearn.model_selection import train_test_split
from tqdm import tqdm, tqdm_notebook

tqdm.pandas()


def extract_and_summarize(df):
    (
        df["HPr5MW"],
        df["HPr5MD"],
        df["HPr5ML"],
        df["APr5MW"],
        df["APr5MD"],
        df["APr5ML"],
    ) = zip(*df.progress_apply(helperFunctions.numWDLInLastFiveMatches, axis=1))
    df["HPrM"], df["APrM"] = zip(
        *df.progress_apply(helperFunctions.numDaysSinceLastMatch, axis=1)
    )

    # impute
    df["B365H"], df["B365D"], df["B365A"] = zip(
        *df.progress_apply(helperFunctions.extractAverageBettingOddsIfMissing, axis=1)
    )
    (
        df["SHS"],
        df["SHST"],
        df["SHY"],
        df["SHR"],
        df["SHTHG"],
        df["SAS"],
        df["SAST"],
        df["SAY"],
        df["SAR"],
        df["SHTAG"],
    ) = zip(*df.progress_apply(helperFunctions.numSTYRHTGInLastFiveMatches, axis=1))
    df["B365pred"] = df.progress_apply(helperFunctions.getBet365prediction, axis=1)
    df["B365HDstDrw"] = df["B365D"] / df["B365H"]
    df["B365ADstDrw"] = df["B365D"] / df["B365A"]
    df["RstDayDiff"] = df["HPrM"] - df["APrM"]

    # we can now drop some of the columns using which we made changes
    columns_to_drop = [
        "B365H",
        "B365D",
        "B365A",
        "HPrM",
        "APrM",
        "AC",
        "AF",
        "AR",
        "AS",
        "AST",
        "AY",
        "HC",
        "HF",
        "HR",
        "HS",
        "HST",
        "HY",
        "HTAG",
        "HTHG",
    ]
    columns_to_drop = [col for col in columns_to_drop if col in df.columns]
    df = df.drop(columns=columns_to_drop)
    return df


link = "https://www.football-data.co.uk/fixtures.csv"
saveLocation = os.path.join("downloaded_data_test", "all.csv")
df = pd.read_csv(link)
df["Div"].unique()
selected_columns = ["AwayTeam", "Date", "HomeTeam", "league", "B365H", "B365D", "B365A"]
leagueNames = [
    "bundesliga",
    "la-liga",
    "ligue-1",
    "premier-league",
    "serie-a",
    "B1",
    "E0",
    "E1",
    "E2",
    "E3",
    "F1",
    "F2",
    "G1",
    "N1",
]
leagueIdentifierDict = {
    "bundesliga": "D1",
    "la-liga": "SP1",
    "ligue-1": "F1",
    "premier-league": "E0",
    "serie-a": "I1",
    "B1": "B1",
    "E0": "E0",
    "E1": "E1",
    "E2": "E2",
    "E3": "E3",
    "F1": "F1",
    "F2": "F2",
    "G1": "G1",
    "N1": "N1",
}

# Assuming your DataFrame has a column named 'Div' that contains the league identifiers
# If your DataFrame has a different column name for league identifiers, replace 'Div' with the correct column name
df["league"] = df["Div"].map(leagueIdentifierDict)

df = df[selected_columns]

test_df = extract_and_summarize(df)
